import java.util.HashSet;

/**
 * @Description:轮转数组 给你一个数组，将数组中的元素向右轮转 k 个位置，其中 k 是非负数。
 * @Date:Create in 15:05  2022/5/27
 */
public class rotate0189 {
    public static void main(String[] args) {
        int[] nums = new int[]{1, 2, 3, 4, 5, 6, 7};
        rotate1(nums, 3);
        for (int num : nums) {
            System.out.println(num);
        }
    }


    //使用额外数组
    public static void rotate1(int[] nums, int k) {
        int n = nums.length;
        int[] newArr = new  int[n];
        for(int i = 0;i<n;++i){
            newArr[(i+k)%n] = nums[i];
        }
        System.arraycopy(newArr,0,nums,0,n);

    }

    //数组翻转
    public static void rotate2(int[] nums, int k) {
        k %= nums.length;
        reverse(nums, 0, nums.length - 1);
        reverse(nums, 0, k - 1);
        reverse(nums, k, nums.length - 1);

    }

    public static void reverse(int[] nums, int start, int end) {
        while (start < end) {
            int temp = nums[start];
            nums[start] = nums[end];
            nums[end] = temp;
            start += 1;
            end -= 1;
        }
    }



}
